package com.hjy.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class PersonListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        Integer onlineNumber = (Integer) servletContext.getAttribute("onlineNumber");
        if (onlineNumber==null){
            onlineNumber = new Integer(1);
        }else {
            int count = onlineNumber.intValue();
            onlineNumber = new Integer(count+1);
        }
        System.out.println(onlineNumber);
        servletContext.setAttribute("onlineNumber",onlineNumber);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        Integer onlineNumber = (Integer) servletContext.getAttribute("onlineNumber");
        if (onlineNumber==null){
            onlineNumber = new Integer(1);
        }else {
            int count = onlineNumber.intValue();
            onlineNumber = new Integer(count-1);
        }
        System.out.println(onlineNumber);
        servletContext.setAttribute("onlineNumber",onlineNumber);
    }
}
